package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.CouponMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class CouponService implements BaseService<Coupon> {

@Resource
private CouponMapper couponMapper;

    @Override
    public PageInfo<Coupon> searchSomeByPage(PageParam<Coupon> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Coupon> couponsList=couponMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Coupon> couponPageInfo = new PageInfo<Coupon>(couponsList);
            return couponPageInfo;
        }else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Coupon> couponsList = couponMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Coupon> couponPageInfo = new PageInfo<Coupon>(couponsList);
            return couponPageInfo;
        }
    }

    @Override
    public Coupon getOneById(String id) {
        Coupon oneById = couponMapper.getOneById(id);
        if(!StringUtils.isEmpty(oneById)){
            return oneById;
        }
        return null;
    }
    @Override
    public Rsp addOne(Coupon coupon) {
        coupon.setId(UUID.randomUUID().toString().replace("-",""));
        int i = couponMapper.addOne(coupon);
        if(i>0){
            return Rsp.success(coupon) ;
        }
        return Rsp.fail();
    }
    @Override
    public Rsp update(Coupon coupon) {
        int i = couponMapper.update(coupon);
        if(i>0){
            return Rsp.success(couponMapper.getOneById(coupon.getId())) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(Coupon coupon) {
        coupon.setDeleted(Num.DELETED_TRUE);

        int i = couponMapper.update(coupon);
        if(i>0){
            return Rsp.success(couponMapper.getOneById(coupon.getId())) ;
        }
        return Rsp.fail();
    }
}
